<?php
/*************************************************************************
Class Pick_Dao_Contact
----------------------------------------------------------------
PICKCO CMS Project
Company: PICKCO  Co., Ltd  
Email: admin@pick.vn
Last updated: 15/07/2008
**************************************************************************/
class Pick_Dao_Contact extends Pick_Core_Objects {
	function Contact($database = '') {
		if(!$database) {
			global $db;
			$this->_db = $db;
		} else $this->_db = $database;
		$this->typeId = " AND typeid = 10";
		$this->table = DB_PREFIX."product";		
	}
	function getObjects($page = 1, $condition = '1>0', $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		if(!$page) $page = 1;
		$start = ($page-1) * (int)$items_per_page;
		$results = $this->select('*', $condition." AND typeid = 10", $sort, $start, $items_per_page);
                
		if($results) {
                    $objectsInfo = array();
                    foreach($results as $key => $result) {			
                            $objectsInfo[] = new newsInfo($result);				
                    }
                    $pages = $this->getNumItems('id', $condition, $items_per_page);
                    $arrInfor = array();
                    $pages['page']      = $page;
                    $pages['start']     = $start;
                    $arrInfor['data']   = $objectsInfo;
                    $arrInfor['pages']  = $pages;
                    return $arrInfor;
		}
		return '';
	}
/*-----------------------------------------------------------------------*
* Function: getObject
* Parameter: WHERE $key
* Return: Array of Info objects
*-----------------------------------------------------------------------*/        
	function getObject($key = '0') {
		$result = $this->getObjects(1,"`id` = '$key'");
		if($result)
			return $result['data'][0];
		return;
	}       
/*-----------------------------------------------------------------------*
* Function: getObjectsFromStringCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromStringCId($page = 1, $cid, $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		return $this->getObjects($page, 'stringcid LIKE \'%"' . $cid . '"%\' AND status = 1', $sort, $items_per_page);
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromCId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromCId($page = 1, $cid, $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		return $this->getObjects($page, 'cid = "' . $cid . '" AND status = 1', $sort, $items_per_page);
	}
/*-----------------------------------------------------------------------*
* Function: getObjectsFromCIdByTime
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromCIdByTime($numDate, $cid, $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		$startDate 	= dateafter(0);
		$endDate 	= dateafter($numDate);
		$conditions = 'cid = "' . $cid.'" AND  date_created >= "'.$startDate.'"'.' AND  date_created <= "'.$endDate.'"';
		return $this->getObjects(1, $conditions . ' AND status = 1', $sort, $items_per_page);
	}  	       
/*-----------------------------------------------------------------------*
* Function: getObjectsFromArrayId
* Parameter: WHERE condition
* Return: Array of Info objects
*-----------------------------------------------------------------------*/
	function getObjectsFromArrayCId($page = 1, $array_id = array(), $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		if(count($array_id)>0){
			$string_id = implode(',',$array_id);
			return $this->getObjects($page, 'cid IN('.$string_id.')' . ' AND status = 1', $sort, $items_per_page);
		}
		return;
	}
			
	function getNumRecordCId($cid) {
		$result = $this->getNumItems('id',"`cid` = '$cid' AND status = 1", $items_per_page = DEFAULT_ROWS_PER_PAGE);
		if($result) return $result;
		return "";
	}	

	function getObjectsFront($page = 1, $condition = '1>0', $sort = array(), $items_per_page = DEFAULT_ROWS_PER_PAGE) {
		return $this->getObjects($page, $condition, array('`date_created`' => 'DESC'), $items_per_page);
	}
	
	function getObjectFromSlug($slug = ''){
		if($slug != ''){
			$object =  $this->getObjects(1, "`slug` = '$slug'" . ' AND status = 1');
			if(is_array($object))
				return $object['data'][0]; # ket qua tra ve se la mot doi tuong duy nhat
		}
		return '';
	}
	
	function comboListObjects($pk = 'id', $name = 'vn_name', $value='', $condition = '1=1', $sort = array('position' => 'ASC')){		
		$results = $this->select("`$pk`,`$name`", $condition, $sort);
		$str = '';
		if($results){
			foreach($results as $key => $result) {
				$str .= "<option value=\"".$result[$pk]."\"".($result[$pk]==$value?" selected":"").">".$result[$name]."</option>";
			}
		}
		return $str;
	}
/*-----------------------------------------------------------------------*
* Function: getRelatedProduct lay san pham cung loai
* Parameter: WHERE catproduct
* Return: Array Bid of manufacturers
*-----------------------------------------------------------------------*/		
	function getRelatedProduct($page = 1, $id, $sort = array(), $items_per_page = 0){
		$news = $this->getObject($id);
		if($news){
			$condition = 'cid = ' . $news->getField('cid') . ' AND id NOT IN('. $id .')';
			return $this->getObjects($page, $condition, $sort, $items_per_page);
		}
		return;
	}
/*-----------------------------------------------------------------------*
* Function: cleanTrash
* Parameter: WHERE condition
* Return: true or false
*-----------------------------------------------------------------------*/
	function cleanTrash($condition = '1=1'){
		$dem = 0;
		$objectDelete = $this->getObjects(1, $condition, '', '');
		foreach($objectDelete as $object){
			$ok1 = $object->deleteAvatar();
			$ok3 = $object->deleteVideo();
			$ok2 = $this->delete("id = " . $object->getField('id'));
			if(!$ok1 && !$ok2 && !$ok3) $dem++;
		}
		return $dem==0?true:false;
	}	
}
?>